package com.tools.ad.adscene.inner;

import android.os.Message;
import android.view.View;

import com.hm.ads.sdk.HmAdsSDK;
import com.hm.adsbuisiness.bean.HmAdUnionPlace;
import com.hm.adsbuisiness.utils.AdUtils;
import com.hm.adsui.card.CommonNativeAdCardView;
import com.hm.base.android.mob.AMApplication;
import com.tools.ad.AdType;
import com.tools.ad.EntranceType;
import com.tools.ad.R;
import com.tools.ad.adscene.AdReportScene;
import com.tools.ad.strategy.IStrategyExecutor;
import com.tools.ad.ui.crad.view.CommonCardView;

/**
 * desc: 普通的原生广告（banner）场景
 * date: 2018/4/18
 * author: ancun
 */

public class CommonScene extends AdReportScene {

    private IStrategyExecutor mExecutor;

    public CommonScene(AMApplication context, EntranceType entranceType) {
        super(context, entranceType);
    }

    @Override
    public boolean load(boolean forceLoad) {
        HmAdUnionPlace adUnionPlace = HmAdsSDK.getInstance().getUnionAd(mEntranceType.getPid());
        if (adUnionPlace == null) {
            return false;
        }

        mExecutor = getADModule().getADStrategyManager().getExecutorByEntranceType(mEntranceType);
        if (forceLoad || mExecutor.check(adUnionPlace)) {
            return loadUnionPlace(adUnionPlace);
        }
        return false;
    }

    @Override
    protected CommonNativeAdCardView initCardView() {
        return new CommonCardView(imContext);
    }

    @Override
    public View getAdView() {
        View adView = super.getAdView();
        if (adView instanceof CommonCardView) {
            if (!AdUtils.isFaceBook(activeAd)) {
                View connerLayout = adView.findViewById(R.id.conner_layout);
                if (connerLayout != null) {
                    connerLayout.setVisibility(View.GONE);
                }
            }
        }
        return adView;
    }

    @Override
    protected void onADLoaded(AdType adType) {
        final Message message = Message.obtain();
//        message.arg2 = adType;
        message.obj = mEntranceType.getName();
        message.what = R.id.msg_ad_common_scene_loaded;
        imContext.handleMobMessage(message);
    }

    @Override
    protected void onAdClose(AdType adType) {
        //release();
    }

    @Override
    protected void onADShow(AdType adType) {
        super.onADShow(adType);
        if (mExecutor != null) {
            mExecutor.record();
        }
    }
}
